#coding:utf-8

from . import db
from werkzeug.security import generate_password_hash,check_password_hash
from sqlalchemy.orm import relationship,backref


class Users(db.Model):
    __tablename__='user'
    id =db.Column(db.Integer,primary_key=True)
    name =db.Column(db.String(255))
    hash_psw=db.Column(db.String(255))
    # password='123'
    #把函数变成属型，就可以直接对齐赋值
    @property
    def password(self):
        #调用这个不让读取
        pass
    #属性复制会调用set方法
    @password.setter
    #路由传过来的psw
    def password(self,psw):
        self.hash_psw=generate_password_hash(psw)

    def verify_psw(self,psw):
        #self是从数据库中取出的对象与页面穿进去的做对比
        return check_password_hash(self.hash_psw,psw)

class Kinds(db.Model):
    __tablename__='t_kinds'
    id=db.Column(db.Integer,primary_key=True)
    name=db.Column(db.String(255))
    courses=relationship('Courses',backref='kind',cascade='delete')

class Courses(db.Model):
    __tablename__='t_courses'
    id= db.Column(db.Integer,primary_key=True)
    title=db.Column(db.String(255))
    abstract=db.Column(db.String(255))
    addtime=db.Column(db.DateTime)
    k_id=db.Column(db.Integer,db.ForeignKey('t_kinds.id'))
    #cascade='delete'删除id对应的所有条数据(子表与父表都跟着删除)
    videos=relationship('Videos',backref='video',cascade='delete')

class Videos(db.Model):
    __tablename__='t_videos'
    id =db.Column(db.Integer,primary_key=True)
    title=db.Column(db.String(255))
    #时长
    time=db.Column(db.Integer)
    #点击次数
    hits=db.Column(db.Integer)
    #描述
    abstract=db.Column(db.String(255))
    #老师id
    t_id=db.Column(db.Integer)
    #课程id
    c_id=db.Column(db.Integer,db.ForeignKey('t_courses.id'))

class Teachers(db.Model):
    __tablename__='t_teachers'
    id=db.Column(db.Integer,primary_key=True)
    name=db.Column(db.String(255))
    abstract=db.Column(db.String(255))
    level=db.Column(db.String(255))
    